//index.js
//获取应用实例
const app = getApp()
import {
  Index
} from 'index-model.js'
var ajax = new Index(); // 实例化

Page({
  data: {
    isKeyboard: true,//是否显示键盘
    specialBtn: false,
    tapNum: false,//数字键盘是否可以点击
    parkingData: false,//是否展示剩余车位按钮
    isFocus: true,//输入框聚焦
    flag: false,//防止多次点击的阀门
    keyboardNumber: '1234567890',
    keyboardAlph: 'QWERTYUP港澳ASDFGHJKL学ZXCVBNM巛',
    keyboard1: '粤京津沪冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵川青藏琼宁渝',
    keyboard2: '',
    keyboard2For: ['查询'],
    keyboardValue: '',
    textArr: [],
    textValue: ['云','A'],// 车牌默认
    HistoryData:[], // 历史车牌数据
    orderStartTime:'' // 二维码开始时间
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let carNum = decodeURIComponent(options.q);
    
    if(carNum != "undefined"){
      let pathUrl = this.getQueryVariable(carNum);
      let newCarNum=pathUrl.car;
      let carArr = [];
      wx.setStorageSync('pathUrl',pathUrl);
      for(let i=0;i<newCarNum.length;i++){
        carArr.push(newCarNum[i]);
      }
      wx.setStorageSync('orderStatusType',1);
      this.setData({
        textValue:carArr,
        orderStartTime:pathUrl.time
      })
      this.tapSpecBtn();
    }else{
      wx.setStorageSync('orderStatusType',0);
    }

    //wx.clearStorage()
    //生命周期函数--监听页面加载
    wx.removeStorageSync('CarOrderData');
  },

 
  // 获取URL 参数
 getQueryVariable(urlStr) {
  if (typeof urlStr == "undefined") {
      var url = decodeURI(location.search); //获取url中"?"符后的字符串
  } else {
      var url = "?" + urlStr.split("?")[1];
  }
  var theRequest = new Object();
  if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      let strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
      }
  }
  return theRequest;
  },


  // 查询事件（完成）
  tapSpecBtn: function (e) {
    var that = this;
    var carNum = that.data.textValue.join("");
    if (that.data.flag || carNum.length < 7) {
      wx.showToast({
        title: '请输入正确车牌',
        icon:'none'
      })
      return false;
    }
    let orderStatus = wx.getStorageSync('orderStatusType');
    // 请求数据
    let meter = {
      "car": carNum,
      "validateTime":that.data.orderStartTime,
      "orderType":orderStatus
    }
    ajax.getFeeByCarData(meter,(res)=>{
      if (res.status == 1){
        let item = res.data;
        if (item.OrderDtos.length > 0){
          wx.setStorageSync("CarOrderData", item);
          wx.navigateTo({
            url:'/pages/orderrecord/index'
          })
        }else{
          wx.removeStorageSync('CarOrderData');
          wx.showModal({
            title: '提示',
            content: '没有未缴费订单',
            showCancel:false,
            success(res) {
              if (res.confirm) {
                //console.log('用户点击确定')
              } else if (res.cancel) {
               // console.log('用户点击取消')
              }
            }
          })
        }
      }else{
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false,
          success(res) {
            if (res.confirm) {
              //console.log('用户点击确定')
            } 
          }
        })
      }
    })
  },

  // 车牌号查询数据修改
  carQueryEditInfo:function(res){
    let that = this;
    let item = res;
    for(let i=0; i<item.data.length;i++){
      let week = that.ateWeek(item.data[i].StartTime);
      let xinStartTime = that.xinStartDate(item.data[i].StartTime);
      let date = that.ateMinus(item.data[i].StartTime, item.data[i].EndTime);
      item.data[i].week = week;
      item.data[i].newsDate = xinStartTime;
      item.data[i].carDay = date;
    }
    //console.log(res);
    return item;
  },

  //结束时间-开始时间 
  ateMinus: function (startDate, endDate) {//startDate:小日期   endDate:大日期
    let date= '';
    let sdate = new Date(startDate);
    let now = new Date(endDate);
    let total  = (now.getTime() - sdate.getTime())/1000;
    let day = parseInt(total / (60 * 60 * 24)); /*计算整数天数*/
    let afterDay = total - day*24*60*60;/*取得算出天数后剩余的秒数*/
    let hours = parseInt(afterDay/(60 * 60));/*计算整数小时数*/
    let afterHour = total - day * 24 * 60 * 60 - hours * 60 * 60;/*取得算出小时数后剩余的秒数*/
    let minutes = parseInt(afterHour/60)/*计算整数分*/
    if (day == "0"){
      day = "";
    }
    date = day+'天'+ hours+'小时' + minutes+'分';
    return date;
  },

  // 周末计算
  ateWeek: function (startDate){
    let weekArray = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    let week = weekArray[new Date(startDate).getDay()];
    return week;
  },
  
  //返回驶入时间新格式
  xinStartDate: function (startDate){
    let date = new Date(startDate);
    let yy = date.getFullYear(); // 年
    let mm = date.getMonth()+1; //月
    let dd = date.getDate(); //日
    let result = yy + '/' + mm +'/' +dd;
    return result;
  },

  //点击页面隐藏键盘事件
  hideKeyboard: function () {
    var that = this;
    if (that.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      if (that.data.textValue) {
        that.setData({
          isKeyboard: false
        })
      } else {
        that.setData({
          isKeyboard: false,
          isFocus: false
        })
      }

    }
  },
  //键盘点击事件
  tapKeyboard: function (e) {
    var that = this;
    //获取键盘点击的内容，并将内容赋值到textarea框中
    var tapIndex = e.target.dataset.index;
    var tapVal = e.target.dataset.val;
    if(tapVal == 'itemName') return;
    var keyboardValue;
    var specialBtn;
    var tapNum;
    if (tapVal == "巛") {
      //说明是删除
      that.data.textArr.pop();
      if (that.data.textArr.length == 0) {
        //说明没有数据了，返回到省份选择键盘
        this.specialBtn = false;
        this.tapNum = true;
        this.keyboardValue = that.data.keyboard1;
      } else if (that.data.textArr.length == 1) {
        //只能输入字母
        this.tapNum = false;
        this.specialBtn = true;
        this.keyboardValue = that.data.keyboard2;
      } else {
        this.specialBtn = true;
        this.tapNum = true;
        this.keyboardValue = that.data.keyboard2;
      }
      that.data.textValue = that.data.textArr;
      //that.data.textValue = that.data.textArr.join("");// 测试使用
      that.setData({
        textValue: that.data.textValue,
        keyboardValue: this.keyboardValue,
        specialBtn: this.specialBtn,
        tapNum: this.tapNum,
      })
      return false
    }
    if (that.data.textArr.length >= 8) {
      return false;
    }
    that.data.textArr.push(tapVal);
    that.data.textValue = that.data.textArr;
    //that.data.textValue = that.data.textArr.join("");// 测试使用
    that.setData({
      textValue: that.data.textValue,
      keyboardValue: that.data.keyboard2,
      specialBtn: true,
    })
    if (that.data.textArr.length > 1) {
      //展示数字键盘
      that.setData({
        tapNum: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //生命周期函数--监听页面显示
    var that = this;
    wx.removeStorageSync('CarOrderData'); // 查询出来的订单数据
    let token =wx.getStorageSync('token');
    if(token = ""){
      ajax.getIndeData((res) => {
        //console.log(res)
        if (res.status == 1) {
          var item = res.data;
          that.setData({
            HistoryData: item
          })
        }
      })
    }
    let orderStatus = wx.getStorageSync('pathUrl'); //car
    var carno = that.data.textValue; 
    if (carno.length < 9) {
      that.setData({
        keyboardValue: that.data.keyboard2,
        specialBtn: true,
        tapNum: true,
        textArr: carno
      })
    } else {
      that.setData({
        keyboardValue: that.data.keyboard1
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})